package com.solution.admin.point.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.solution.admin.point.entity.PointRuleEntity;
import com.solution.admin.point.service.PointRuleService;
import com.solution.admin.settings.entity.KeyMapEntity;
import com.solution.admin.settings.service.KeyMapService;
import com.solution.common.constants.PublicStatus.STATUS;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.string.StringUtil;

/**
 * 积分规则设置信息web控制层
 * 
 * @author xu_cc
 * @date 2017-10-20 14:25:42
 */
@RequestMapping("/pointRule")
@Controller
public class PointRuleController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PointRuleController.class);

    @Autowired
    private PointRuleService pointRuleService;

    @Autowired
    KeyMapService keyMapService;

    /**
     * 跳转积分规则设置信息查询页面
     * 
     * @param PointRule
     *            接收界面查询条件参数
     * @param request
     *            请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
     * @date 2017-10-20 14:25:42
     */
    @RequestMapping("/toPointRulePage")
    public String toListPointRulePage(HttpServletRequest request, Model model) {
        PointRuleEntity pointRule = pointRuleService.getEnablePointRule();
        model.addAttribute("pointRule", pointRule);
        List<KeyMapEntity> typeList = keyMapService.listKeyMap("pointClearRuleType");
        model.addAttribute("pointClearRuleTypeList", typeList);
        return "point/pointRule_form";
    }

    /**
     * 保存积分规则设置信息
     * 
     * @param request
     *            请求对象
     * @param pointRuleEntity
     *            积分规则设置信息实体
     * @return
     * @author xu_cc
     * @date 2017-10-20 14:25:42
     */
    @RequestMapping("/savePointRule")
    public String savePointRule(HttpServletRequest request, PointRuleEntity pointRuleEntity, Model model) {
        try {
            if (null == pointRuleEntity) {
                model.addAttribute("msg", "积分规则保存失败，信息输入不正确！");
            } else {
                pointRuleEntity.setCreater(getUserName());
                pointRuleEntity.setStatus(STATUS.ENABLED.getId());
                pointRuleEntity.setModifier(getUserName());
                if (StringUtil.isEmpty(pointRuleEntity.getRuleType()) || StringUtil.isEmpty(pointRuleEntity.getExpireDate())
                        || StringUtil.isEmpty(pointRuleEntity.getIntro())) {
                    model.addAttribute("msg", "积分规则保存失败，信息输入不完整！");
                } else {
                    PointRuleEntity oldPointRule = pointRuleService.getEnablePointRule();
                    // 根据到期日期、规则类型、规则描述判断是否修改
                    if (null != oldPointRule && oldPointRule.getRuleType().equals(pointRuleEntity.getRuleType())
                            && oldPointRule.getExpireDate().equals(pointRuleEntity.getExpireDate())
                            && oldPointRule.getIntro().equals(pointRuleEntity.getIntro())) {
                        // 没有变化，直接返回成功
                        model.addAttribute("msg", "积分规则保存成功！");
                    } else {
                        pointRuleService.savePointRule(pointRuleEntity);
                        model.addAttribute("msg", "积分规则保存成功！");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "积分规则保存异常！" + e.getMessage());
        }
        return toListPointRulePage(request, model);
    }

}
